﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using XunPan.Web;
using SalePartial.BLL;
using Telerik.Web.UI;
using SalePartial.Utility;

public partial class Roles_ManageRoles_power : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            // 检查用户权限但不存入使用记录
            LoginUserType type = RoleControlManager.checkUserAuthority(User.Identity.Name);
            if (type != LoginUserType.ADMIN)
            {
                Response.Redirect(GlobalSetting.UrlHomePage, true);
            }

        }
    }

    // Check_Power() 检查用户权限
    private void Check_Power(string f_power)
    {

        try
        {
            if (SecurityUtility.Check_Power(Session["UserName"].ToString(), Session["mg_power"].ToString(), f_power) > 0)
            {
                Response.Redirect("~/Error.aspx?ErrCode=1");
            }
        }
        catch
        {
            Response.Redirect("~/Error.aspx?ErrCode=2");
        }
    }

    protected void gv_Func_Power_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowType == DataControlRowType.DataRow))
        {
            //DataRowView DataRV = (DataRowView)e.Row.DataItem;

            //RadioButton rb_en = (RadioButton)e.Row.Cells[2].FindControl("rb_open");
            //RadioButton rb_di = (RadioButton)e.Row.Cells[2].FindControl("rb_close");
            //Label lb_fi_no1 = (Label)e.Row.Cells[2].FindControl("lb_fi_no1");
            //Label lb_fi_no2 = (Label)e.Row.Cells[2].FindControl("lb_fi_no2");

            //// 将主功能代码及子功能代码存入隐藏的 Label 控件，
            //// 因为 DataItem 在 RowDataBound 事件之后，不会被保存。
            //lb_fi_no1.Text = DataRV["fi_no1"].ToString();
            //lb_fi_no2.Text = DataRV["fi_no2"].ToString();

            //switch (lb_enable.Text)
            //{
            //    case "0":		// 状态为‘全部禁止’
            //        rb_en.Checked = false;
            //        rb_di.Checked = true;
            //        break;
            //    case "1":		// 状态为‘全部开放’
            //        rb_en.Checked = true;
            //        rb_di.Checked = false;
            //        break;
            //    default:		// 状态为‘自定义’
            //        if (DataRV["is_enable"].ToString() == "0")
            //        {
            //            rb_en.Checked = false;
            //            rb_di.Checked = true;
            //        }
            //        else
            //        {
            //            rb_en.Checked = true;
            //            rb_di.Checked = false;
            //        }
            //        break;
            //}
        }
    }

    // ‘确定保存’按钮的 Click 事件处理函数。
    protected void lb_ok_Click(object sender, EventArgs e)
    {
        string SqlString = "";
        string fi_no1 = "", fi_no2 = "";
        SqlCommand Sql_Command = new SqlCommand();

        if (rdcmbRole.SelectedValue == string.Empty)
        {
            return;
        }

        using (SqlConnection sql_conn = new SqlConnection(ConfigurationManager.ConnectionStrings["XunPanConnectionString"].ConnectionString))
        {
            sql_conn.Open();

            // 先清除该位人员所有的权限，然后再重新新增。
            SqlString = "Delete Func_Power Where Role_mg_id = @mg_id";

            Sql_Command.Connection = sql_conn;
            Sql_Command.CommandText = SqlString;
            Sql_Command.Parameters.AddWithValue("mg_id", rdcmbRole.SelectedValue);

            Sql_Command.ExecuteNonQuery();

            Sql_Command.Dispose();

            #region 处理 GridView 里面的每一笔数据是否赋予使用权限
            foreach (GridDataItem dataItem in rgridMenuRole.Items)
            {

                //if (item.ItemType == GridItemType.Item)
                //{
                RadioButton rb_en = (RadioButton)dataItem["is_enable"].FindControl("rb_open");

                    // 有‘开放’权限时，才要存入数据库。
                    if (rb_en.Checked)
                    {
                        Label lb_fi_no1 = (Label)dataItem["is_enable"].FindControl("lb_fi_no1");
                        Label lb_fi_no2 = (Label)dataItem["is_enable"].FindControl("lb_fi_no2");

                        fi_no1 = lb_fi_no1.Text;
                        fi_no2 = lb_fi_no2.Text;

                        SqlString = "Insert Into Func_Power (Role_mg_id, fi_no1, fi_no2, is_enable)";
                        SqlString = SqlString + " Values (@mg_id, @fi_no1, @fi_no2, 1);";

                        Sql_Command.Parameters.Clear();
                        Sql_Command.CommandText = SqlString;
                        Sql_Command.Parameters.AddWithValue("mg_id", rdcmbRole.SelectedValue);
                        Sql_Command.Parameters.AddWithValue("fi_no1", fi_no1);
                        Sql_Command.Parameters.AddWithValue("fi_no2", fi_no2);

                        Sql_Command.ExecuteNonQuery();

                        Sql_Command.Dispose();
                    //}
                }
            }
            #endregion
        }
        lt_show.Text = "<script language=javascript>alert('权限设置完成！');</script>";
        //lt_show.Text = "<script language=javascript>alert('权限设置完成！');location.replace('ManageRoles_Content.aspx" + lb_page.Text + "');</script>";
    }

    protected void bn_all_open_Click(object sender, EventArgs e)
    {
        // 状态设为‘全部开放’
        lb_enable.Text = "1";
        rgridMenuRole.DataBind();
    }

    protected void bn_all_close_Click(object sender, EventArgs e)
    {
        // 状态设为‘全部禁止’
        lb_enable.Text = "0";
        rgridMenuRole.DataBind();
    }

    protected void rb_open_CheckedChanged(object sender, EventArgs e)
    {
        // 状态设为‘自定义’
        lb_enable.Text = "2";
    }

    protected void rb_close_CheckedChanged(object sender, EventArgs e)
    {
        // 状态设为‘自定义’
        lb_enable.Text = "2";
    }
    protected void rdcmbRole_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {

    }

    protected void rgridMenuRole_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        //rgridMenuRole.DataBind();
        //rgridMenuRole.DataSource = sqs_Func_Power;
    }
    protected void RadButton1_Click(object sender, EventArgs e)
    {
        // 状态设为‘自定义’
        lb_enable.Text = "2";

        lb_mg_name.Text = rdcmbRole.SelectedValue;
        sqs_Func_Power.SelectParameters["mg_id"].DefaultValue = rdcmbRole.SelectedValue;
        rgridMenuRole.DataBind();
    }
    protected void rgridMenuRole_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {

        if (e.Item is GridHeaderItem)
        {
            GridHeaderItem header = (GridHeaderItem)e.Item;
            //header["CompanyName"].Text = "TEST";
        }
        else if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = (GridDataItem)e.Item;
            DataRowView DataRV = (DataRowView)e.Item.DataItem;

            RadioButton rb_en = (RadioButton)dataItem["is_enable"].FindControl("rb_open");
            RadioButton rb_di = (RadioButton)dataItem["is_enable"].FindControl("rb_close");
            Label lb_fi_no1 = (Label)dataItem["is_enable"].FindControl("lb_fi_no1");
            Label lb_fi_no2 = (Label)dataItem["is_enable"].FindControl("lb_fi_no2");

            
            // 将主功能代码及子功能代码存入隐藏的 Label 控件，
            // 因为 DataItem 在 RowDataBound 事件之后，不会被保存。
            if (DataRV != null)
            {
                lb_fi_no1.Text = DataRV.Row["fi_no1"].ToString();
                lb_fi_no2.Text = DataRV.Row["fi_no2"].ToString();
            }

            switch (lb_enable.Text)
            {
                case "0":		// 状态为‘全部禁止’
                    rb_en.Checked = false;
                    rb_di.Checked = true;

                    break;
                case "1":		// 状态为‘全部开放’
                    rb_en.Checked = true;
                    rb_di.Checked = false;
                    break;
                default:		// 状态为‘自定义’
                    if (DataRV != null && DataRV.Row["is_enable"].ToString() == "0")
                    {
                        rb_en.Checked = false;
                        rb_di.Checked = true;
                    }
                    else
                    {
                        rb_en.Checked = true;
                        rb_di.Checked = false;
                    }
                    break;
            }
        }

    }
}
